/*
 * _______________#########_______________________ 
 * ______________############_____________________ 
 * ______________#############____________________ 
 * _____________##__###########___________________ 
 * ____________###__######_#####__________________ 
 * ____________###_#######___####_________________ 
 * ___________###__##########_####________________ 
 * __________####__###########_####_______________ 
 * ________#####___###########__#####_____________ 
 * _______######___###_########___#####___________ 
 * _______#####___###___########___######_________ 
 * ______######___###__###########___######_______ 
 * _____######___####_##############__######______ 
 * ____#######__#####################_#######_____ 
 * ____#######__##############################____ 
 * ___#######__######_#################_#######___ 
 * ___#######__######_######_#########___######___ 
 * ___#######____##__######___######_____######___ 
 * ___#######________######____#####_____#####____ 
 * ____######________#####_____#####_____####_____ 
 * _____#####________####______#####_____###______ 
 * ______#####______;###________###______#________ 
 * ________##_______####________####______________ 
 * 
 * @Author: ipk518 18163976442@163.com
 * @Date: 2024-10-22 16:53:05
 * @LastEditors: ipk518 18163976442@163.com
 * @LastEditTime: 2024-10-22 17:05:03
 * @FilePath: /etws-sxk2307/src/dataShare/queueShare.c
 * @Description: 
 * ************佛祖保佑************
 * Copyright (c) 2024 by etws@quyujiang, All Rights Reserved. 
 */

#include "queueShare.h"


queueShare::queueShare(int64_t maxSize)
{
    if (maxSize > 0) {
        m_maxSize = maxSize;
    } else {
        m_maxSize = 0;
    }
	memset(m_buffer, 0, sizeof(m_buffer));
}

queueShare::~queueShare()
{
    removeAll();
}

bool queueShare::dataIsValid()
{
    lock_guard<std::mutex> lock(m_dataMtx);
    return (!m_dataQue.empty());
}

int64_t queueShare::dataSize()
{
    lock_guard<std::mutex> lock(m_dataMtx);
    return m_dataQue.size();
}

int queueShare::recvfrom(unsigned char *data,size_t len)
{
    size_t ret = m_maxSize - m_dataQue.size();
    lock_guard<std::mutex> lock(m_dataMtx);
    size_t readLen = (len <= ret)?len :ret;
    for (size_t i=0; i < readLen; i++) {
        m_dataQue.push(data[i]);
    }
    return readLen;
}
uint8_t queueShare::take()
{
    static uint8_t ret = 0;
    lock_guard<std::mutex> lock(m_dataMtx);
    if (m_dataQue.empty()) {
        return 0;
    }
    ret = m_dataQue.front();
    m_dataQue.pop();
    return ret;
}

int queueShare::take(unsigned char *data,size_t len)
{
    size_t ret = m_maxSize - m_dataQue.size();
    lock_guard<std::mutex> lock(m_dataMtx);
    size_t readLen = (len <= ret)?len :ret;
    for (size_t i=0; i < readLen; i++) {
        data[i] = m_dataQue.front();
        m_dataQue.pop();
    }
    return readLen;
}

void queueShare::remove(size_t len)
{
    size_t ret = m_maxSize - m_dataQue.size();
    lock_guard<std::mutex> lock(m_dataMtx);
    size_t Len = (len <= ret)?len :ret;
    for (size_t i=0; i < Len; i++) {
        m_dataQue.pop();
    }
}

void queueShare::removeAll()
{
    lock_guard<std::mutex> lock(m_dataMtx);
    while (  !m_dataQue.empty() ) {
        m_dataQue.pop();
    }
}